<template>
  <div class="xtx-skeleton" :style="{ width, height }" :class="[animate]">
    <div class="block" :style="{ backgroundColor: bg }"></div>
  </div>
</template>

<script setup>
// 组合式API中定义 props
import { toRefs } from 'vue'
const props = defineProps({
  width: {
    type: String,
    default: '100px'
  },
  height: {
    type: String,
    default: '40px'
  },
  bg: {
    type: String,
    default: 'red'
  },
  // 支持 d1 和 d2类型动画
  animate: {
    type: String,
    default: 'd1'
  }
})
// 使用toRefs 才能解构出响应式数据
const { width, height, bg, animate } = toRefs(props)

</script>

<style lang="scss" scoped>
.xtx-skeleton {
  display: inline-block;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  &.d1 {
    animation: shan 1s linear infinite alternate;
  }
  &.d2 {
    .block::after {
      content: "";
      position: absolute;
      width: 50%;
      height: 100%;
      background: linear-gradient(
        to left,
        rgba(255, 255, 255, 0) 0,
        rgba(255, 255, 255, 0.3) 50%,
        rgba(255, 255, 255, 0) 100%
      );
      left: 0;
      top: 0;
      animation: shan2 1.5s ease 0s infinite;
      transform: skewX(-45deg);
    }
  }
  .block {
    width: 100%;
    height: 100%;
    border-radius: 2px;
  }
}

@keyframes shan {
  to {
    opacity: 0.2;
  }
  from {
    opacity: 1;
  }
}

@keyframes shan2 {
  0% {
    left: -100%;
  }
  100% {
    left: 120%;
  }
}
</style>
